#include "itg3205.h"

//************初始化L3G4200D*********************************

#define	ITG3205_Addr   0xD0	  //定义器件在IIC总线中的从地址,根据ALT  ADDRESS地址引脚不同修改

//定义ITG3205内部地址********************
#define WHO	    0x00
#define	SMPL	0x15
#define DLPF	0x16
#define INT_C	0x17
#define INT_S	0x1A
#define	TMP_H	0x1B
#define	TMP_L	0x1C
#define	GX_H	0x1D
#define	GX_L	0x1E
#define	GY_H	0x1F
#define	GY_L	0x20
#define GZ_H	0x21
#define GZ_L	0x22
#define PWR_M	0x3E
//****************************
unsigned char TX_DATA[4];  	 //显示据缓存区
static unsigned char BUF[10];       //接收数据缓存区
char  test=0; 				 //IIC用到
static short T_X,T_Y,T_Z,T_T;		 //X,Y,Z轴，温度


static void Single_Write(u8 addr ,u8 REG_Address,u8 REG_data)
{
Sensors_I2C_WriteRegister(addr,REG_Address,1,(unsigned char *)REG_data);//测量范围,正负16g，13位模式
}
static u8 tmp;
unsigned char Single_Read(unsigned char SlaveAddress,unsigned char REG_Address)
{
   
    Sensors_I2C_ReadRegister(SlaveAddress,REG_Address,1,&tmp);//OUT_X_L_A
    return tmp;
}

//初始化ITG3205，根据需要请参考pdf进行修改************************
void Init_ITG3205(void)
{
   Single_Write(ITG3205_Addr,PWR_M, 0x80);   //
   Single_Write(ITG3205_Addr,SMPL, 0x07);    //
   Single_Write(ITG3205_Addr,DLPF, 0x1E);    //±2000°
   Single_Write(ITG3205_Addr,INT_C, 0x00 );  //
   Single_Write(ITG3205_Addr,PWR_M, 0x00);   //
}

//******读取ITG3205数据****************************************
void READ_ITG3205(void)
{
   BUF[0]=Single_Read(ITG3205_Addr,GX_L); 
    printf("\r\nT_XXXX:%d\r\n",Single_Read(ITG3205_Addr,GX_L));
   BUF[1]=Single_Read(ITG3205_Addr,GX_H);
     printf("\r\nT_XXXX:%d\r\n",Single_Read(ITG3205_Addr,GX_H));

   T_X=	(BUF[1]<<8)|BUF[0];
   T_X/=14.375; 						   //读取计算X轴数据

   BUF[2]=Single_Read(ITG3205_Addr,GY_L);
   BUF[3]=Single_Read(ITG3205_Addr,GY_H);
   T_Y=	(BUF[3]<<8)|BUF[2];
   T_Y/=14.375; 						   //读取计算Y轴数据
   BUF[4]=Single_Read(ITG3205_Addr,GZ_L);
   BUF[5]=Single_Read(ITG3205_Addr,GZ_H);
   T_Z=	(BUF[5]<<8)|BUF[4];
   T_Z/=14.375; 					       //读取计算Z轴数据

   BUF[6]=Single_Read(ITG3205_Addr,TMP_L); 
   BUF[7]=Single_Read(ITG3205_Addr,TMP_H); 
   T_T=(BUF[7]<<8)|BUF[6];
   T_T = (short)(35+ ((double) (T_T + 13200)) / 280);// 读取计算出温度
   printf("\r\nT_X:%d\tT_Y:%d\tT_Z:%d\tT_T:%d\t\r\n",T_X,T_Y,T_Z,T_T);
}



